package persistencia;

import java.util.LinkedList;
import java.util.Queue;

public class Buffer {

	private Queue<Object> cola;
	private Thread crawler;
	private BD_operaciones base_datos;
	
	public Buffer() 
	{
		cola=new LinkedList<Object>();
	}

	public boolean isEmpty()
	{
		return cola.isEmpty();
	}
	
	public boolean isFull()
	{
		return cola.size()>=Integer.MAX_VALUE-1;
	}
	
	@SuppressWarnings("deprecation")
	public synchronized Object get() 
	{
		Object res=null;
			
		//try 
		//{
		/*	if(cola.isEmpty())
			{	
				//base_datos.wait();
				//base_datos.parar();
				base_datos.suspend();
				crawler.resume();
			}
			else */
				res=cola.poll();
				System.out.println("Objeto extraido del buffer");
		//} 
	/*	catch (InterruptedException e) 
		{
			e.printStackTrace();
		}
*/		
		
		
		return res;
	}
	
	/**
	 * Devuelve true si se ha insertado correctamente
	 * */
	public synchronized boolean put(Object obj) throws InterruptedException
	{
		boolean res=false;
		
		try
		{
			cola.add(obj);
			res=true;
			System.out.println("Objeto anadido al buffer");
			
		}
		catch(IllegalStateException e)
		{
			//crawler.suspend();	
			e.printStackTrace();
			//base_datos.resume();
		}	
		
		return res;
	}
	
	public Thread getCrawler() {
		return crawler;
	}

	public void setCrawler(Thread crawler) {
		this.crawler = crawler;
	}

	public Thread getBase_datos() {
		return base_datos;
	}

	public void setBase_datos(BD_operaciones base_datos) {
		this.base_datos = base_datos;
	}	
}
